package com.bobo.web.provider.service.impl;

import com.bobo.web.api.domain.video.*;
import com.bobo.web.api.vo.PictureVO;
import com.bobo.web.api.vo.VideoVO;
import com.bobo.web.api.vo.base.ResponseResult;
import com.bobo.web.provider.dao.video.CommentMapper;
import com.bobo.web.provider.dao.video.PictureMapper;
import com.bobo.web.provider.dao.video.VideoMapper;
import com.bobo.web.provider.service.VideoAndPictureService;
import com.bobo.web.provider.util.QiNiuUtils;
import com.bobo.web.provider.util.commutils.GeneralNumUtil;
import com.qiniu.common.QiniuException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoAndPictureServiceImpl implements VideoAndPictureService {
    private static Logger LOGGER = LoggerFactory.getLogger(VideoAndPictureServiceImpl.class.getSimpleName());
    @Resource
    private QiNiuUtils qiNiuUtils;
    @Resource
    private PictureMapper pictureMapper;
    @Resource
    private VideoMapper videoMapper;
    @Resource
    private CommentMapper commentMapper;
    @Value("${basketball.qi.niu.domain}")
    public String BASKETBALL_QI_NIU_DOMAIN_OF_BUCKET;

    @Override
    public ResponseResult<VideoTokenDto> getToken() throws Exception {
        ResponseResult<VideoTokenDto> responseResult = new ResponseResult<>();
        responseResult.setData(qiNiuUtils.getTokenShuiYin());
        return responseResult;
    }

    @Override
    public ResponseResult<String> savePicture(PictureVO pictureVO) throws Exception {
        LOGGER.info("保存图片开始");
        ResponseResult<String> result = new ResponseResult<>();
        PictureDO pictureDO = new PictureDO();
        pictureDO.setGameId(pictureVO.getGameId());
        pictureDO.setPicUrl(BASKETBALL_QI_NIU_DOMAIN_OF_BUCKET + pictureVO.getPicUrl()+5);
        int i = pictureMapper.insertPicture(pictureDO);
        result.setData(pictureDO.getPicUrl());
        return result;
    }

    @Override
    public ResponseResult<String> transcoding(VideoInfoDTO videoInfoDTO) throws QiniuException {
        ResponseResult<String> result = new ResponseResult<>();
        //调取转码接口
        String persistentId = qiNiuUtils.transcoding(videoInfoDTO.getKey());
        videoInfoDTO.setPersistentId(persistentId);
        //保存视频信息
        VideoDO videoDO = new VideoDO();
        videoDO.setGameId(System.currentTimeMillis() + "");
        videoDO.setVKey(videoInfoDTO.getKey());
        videoDO.setDuration(videoInfoDTO.getDuration());
        videoDO.setFormat(videoInfoDTO.getAttFormat());
        videoDO.setPersistentid(persistentId);
        videoDO.setTranscoding(1);
        videoDO.setVisitCount(120);
        videoDO.setFabulousCount(80);
        videoDO.setCommentCount(12);
        videoDO.setVideoName(videoInfoDTO.getName());
        videoDO.setVideoUrlPlay(BASKETBALL_QI_NIU_DOMAIN_OF_BUCKET + videoInfoDTO.getKey() + ".mp4");
        int i = videoMapper.insertSelective(videoDO);
        result.setData(persistentId);
        return result;
    }

    @Override
    public void dealNotifyData(QiNiuPersistentNotifyDto qiNiuPersistentNotifyDto) {
        if (qiNiuPersistentNotifyDto.getCode() != 0) {
            return;
        }
        /**
         * 更新视频记录表状态
         */
        String persistentId = qiNiuPersistentNotifyDto.getId();
    }

    @Override
    public void addFabulous(VideoInfoDTO videoInfoDTO) {
        VideoDO videoDO1 = videoMapper.selectByPrimaryKey(videoInfoDTO.getKey());
        if(videoDO1==null){
            return;
        }
        VideoDO videoDO = new VideoDO();
        videoDO.setVKey(videoInfoDTO.getKey());
        videoDO.setFabulousCount(videoDO1.getFabulousCount()+1);
        videoMapper.updateByPrimaryKeySelective(videoDO);
    }

    @Override
    public ResponseResult<String> addComment(CommentDO commentDO) {
        commentDO.setCommentId(GeneralNumUtil.getGeneralNum("COMM"));
        int i = commentMapper.insertCommentSelective(commentDO);
        //增加评论数
        VideoDO videoDO = new VideoDO();
        videoDO.setVKey(commentDO.getVideoKey());
        videoDO.setCommentCount(1);
        videoMapper.updateByPrimaryKeySelective(videoDO);
        return ResponseResult.success();
    }
    @Override
    public ResponseResult<List<PictureVO>> getPictureList(PictureDO params) {
        List<PictureVO> voList = new ArrayList<>();
        List<PictureDO> pictureDOS = pictureMapper.selectPictureByPrimaryKey(params);
        for (PictureDO pictureDO:pictureDOS){
            PictureVO pictureVO1 = new PictureVO();
            pictureVO1.setGameId(pictureDO.getGameId());
            pictureVO1.setCreateTime(pictureDO.getCreateTime());
            pictureVO1.setPicType(pictureDO.getPicType());
            pictureVO1.setPicUrl(pictureDO.getPicUrl());
            voList.add(pictureVO1);
        }
        return ResponseResult.success(voList);
    }

    @Override
    public ResponseResult<List<VideoVO>> getVideoList(String gameId) {
        List<VideoVO> voList = new ArrayList<>();
        List<VideoDO> videoDOS = videoMapper.selectVideoByPrimaryKey(gameId);
        for (VideoDO videoDO:videoDOS){
            VideoVO videoVO = new VideoVO();
            videoVO.setGameId(videoDO.getGameId());
            videoVO.setCreateTime(videoDO.getCreateTime());
            videoVO.setVideoUrlPlay(videoDO.getVideoUrlPlay());
            videoVO.setVideoName(videoDO.getVideoName());
            videoVO.setVisitCount(videoDO.getVisitCount());
            voList.add(videoVO);
        }
        return ResponseResult.success(voList);
    }
}
